{extend name="layout/detail" /}
{block name="content"}
<div class="td-content-wrap p20">
    <div class="td-content-header">
        <h1>数据库管理</h1>
    </div>
    <hr>
    <div class="panel no-border">
        <a class="layui-btn" onclick="go_return_url();">返回</a>
        <span class="mid"></span>
        <a class="layui-btn" onclick="file_list();">备份列表</a>
        <a class="layui-btn layui-btn-warm backup"><i class="fa fa-copy"></i> 立即备份</a>
        <a class="layui-btn optimize_batch"><i class="fa fa-gavel"></i> 优化表</a>
        <a class="layui-btn layui-btn-danger repair_batch"><i class="fa fa-gear"></i> 修复表</a>
    </div>
    <div class="space-20"></div>
    <blockquote class="layui-elem-quote layui-quote-nm">
        提示：
        <br>
        1、您对可变长度的行进行了大量的更新，您也不须要常常执行优化，每周一次或每月一次就可以，仅仅对特定的表执行。
        <br>
        2、在备份、优化或者修复执行过程中，MySQL会锁定表
    </blockquote>
    <div class="hr-line-dashed"></div>
    <table id="data_table" lay-filter="data_table"></table>
</div>
<script type="text/html" id="toobar">
    <a class="layui-btn layui-btn-xs" lay-event="optimize"><i class="fa fa-gavel"></i> 优化表</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="repair"><i class="fa fa-gear"></i> 修复表</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="table_info"><i class="fa fa-gear"></i> 编辑</a>
</script>
{/block}
{block name="script"}
<script type="text/javascript">
    layui.use(['table', 'form', 'layer', 'jquery', 'utils', 'laytpl', 'element'], function () {
        var table = layui.table, form = layui.form,
            layer = layui.layer, $ = layui.jquery,
            utils = layui.utils, laytpl = layui.laytpl,
            element = layui.element;

        set_return_url();

        var data_table = table.render({
            elem: '#data_table',
            height: 'full-120',
            page: false,
            url: "{:url('data/index')}?t=" + new Date().getTime(),
            cols: [[
                {checkbox: true},
                {field: 'name', title: '表名', align: 'center', width: 200},
                {
                    field: 'rows', title: '数据量', align: 'center', width: 180,
                    templet: '<div>【{{ d.rows}}】条记录</div>'
                },
                {
                    field: 'data_length', title: '数据大小', align: 'center', width: 150,
                    templet: '<div>{{ layui.utils.formatFileSize(d.data_length)}}</div>'
                },
                {
                    field: 'add_time', title: '创建时间', align: 'center', width: 180,
                    templet: '<div>{{ layui.utils.toDateString(d.add_time*1000) }}</div>'
                },
                {
                    title: '备份状态', align: 'center', width: 120,
                    templet: '<div>等待备份...</div>'
                },
                {title: '操作', align: 'center', width: 260, toolbar: '#toobar'}
            ]]
        });

        table.on('tool(data_table)', function (obj) {
            var row = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent === 'optimize') {
                ajax_optimize(row.name);
            }

            if (layEvent === 'repair') {
                ajax_repair(row.name);
            }

            if (layEvent === 'table_info') {
                table_info(row);
            }
        });

        // 获取批量选中的行，包含表名name
        function getSelectedRow() {
            var checkStatus = table.checkStatus('data_table');
            var checkgroup = [];
            if (checkStatus.data.length < 1) {
                layer.msg("至少选择一行", {icon: 5, anim: 6, offset: 't'});
                return false;
            }
            checkStatus.data.forEach(function (v) {
                checkgroup.push(v.name);
            });
            return checkgroup.join(',');
        }

        $(function () {
            $('.backup').on('click', function () {
                var ids = getSelectedRow();
                console.log(ids);
                if (ids !== false) {
                    $.ajax({
                        url: "{:url('data/backup')}?t=" + new Date().getTime(),
                        type: 'post',
                        data: 'ids=' + ids,
                        dataType: 'json',
                        beforeSend: function () {
                            loading = layer.msg('数据库备份中，请稍后……', {
                                icon: 16, shade: 0.3, shadeClose: false, time: 0
                            });
                        },
                        success: function (data) {
                            layer.close(loading);
                            if (data.code === 1) {
                                layer.msg('数据表备份成功，马上跳转到备份列表页……', {icon: 1, time: 2000, offset: 't'}, function () {
                                    window.location.href = data.url;
                                })
                            } else {
                                layer.msg(data.msg, {offset: 't', icon: 2});
                            }
                        }
                    })
                }
            });
            $('.optimize_batch').on('click', function () {
                var ids = getSelectedRow();
                if (ids !== false) {
                    ajax_optimize(ids);
                }
            });
            $('.repair_batch').on('click', function () {
                var ids = getSelectedRow();
                if (ids !== false) {
                    ajax_repair(ids);
                }
            })
        })

        let fn = {
            file_list: function () {
                window.open('{:url("file_list")}', "_self");
            },
            ajax_optimize: function (ids) {
                $.post("{:url('data/optimize')}" + '?t=' + new Date().getTime(), {ids: ids}, function (data) {
                    if (data.code === 1) {
                        layer.msg('数据表优化成功', {icon: 1, time: 1000, offset: 't'});
                    } else {
                        layer.msg(data.msg, {offset: 't', icon: 2});
                    }
                }, 'json');
            },
            // 修复表函数
            ajax_repair: function (ids) {
                $.post("{:url('data/repair')}" + '?t=' + new Date().getTime(), {ids: ids}, function (data) {
                    if (data.code === 1) {
                        layer.msg('数据表修复成功', {icon: 1, time: 1000, offset: 't'});
                    } else {
                        layer.msg(data.msg, {offset: 't', icon: 2});
                    }
                }, 'json');
            },
            // 编辑字段
            table_info: function (data) {
                console.log(data);
                window.open('{:url("table_info")}?table=' + data.name, "_self");
            }
        }

        win_exp(fn);

    });


</script>
{/block}